<template>
  <div>
    <el-dialog
      modal-append-to-body
      append-to-body
      lock-scroll
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="850px"
      >
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="100px" 
        :inline="false"
        autocomplete="off"
        v-if="!pload"
        >
        <el-row>
        	<el-col :md="10">
		        <el-form-item label="所属分类" prop="classifyId" :rules="[rule[0]]">
						  <cateselect v-model="selectTable.classifyId"></cateselect>
		        </el-form-item>
        	</el-col>
          <el-col :md="7">
            <el-form-item label="初始点击量" prop="hits" :rules="[rule[0]]">
              <el-input-number v-model="selectTable.hits" controls-position="right" style="width: 120px;" :min="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :lg="7">
            <el-form-item label="作者/来源" prop="author" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.author" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
				</el-row>
        <el-row>
        	<el-col :span="24">
		        <el-form-item label="标题" prop="title" :rules="[rule[0]]">
		          <el-input placeholder="请输入" v-model="selectTable.title" maxLength="100"></el-input>
		        </el-form-item>
        	</el-col>
          <el-col :span="16">
            <el-form-item label="摘要" prop="summary">
              <el-input type="textarea" placeholder="请输入摘要" v-model="selectTable.summary" maxLength="200"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      
        <el-row>
        	<el-col :span="24">
		        <el-form-item label="封面" prop="coverNumber" :rules="[rule[0]]">
						  <el-radio-group v-model="selectTable.coverNumber">
						    <el-radio :label="index" v-for="(i,index) in ['无图','单图','三图']" :key="index">{{i}}</el-radio>
						  </el-radio-group>
		        </el-form-item>
        	</el-col>
        </el-row>
        
        <el-row v-if="selectTable.coverNumber===1">
        	<el-col :lg="12" :md="24">
		        <el-form-item label="图片" prop="imgUrl">
		          <singleImage v-model="selectTable.imgUrl"></singleImage>
		        </el-form-item>
        	</el-col>
        </el-row>
        
        <el-row v-if="selectTable.coverNumber===2">
        	<el-col>
		        <el-form-item label="图片" prop="imgUrl">
		          <multipleImage v-model="selectTable.imgUrl" :max="3"></multipleImage>
		        </el-form-item>
        	</el-col>
        </el-row>
        
        <el-form-item label="内容" prop="detail" :rules ="[rule[0]]">
          <editor v-model="selectTable.detail" :config="{initialFrameHeight:100}"></editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {artSave,artModify,artGetById} from '@/utils/weApi'
import singleImage from '@/components/Upload/singleImage'
import multipleImage from '@/components/Upload/multipleImage'
import cateselect from './cateSelect'
export default {
  components: {
  	editor : () => import('@/components/Ueditor'),
  	singleImage,
  	multipleImage,
  	cateselect
  },
  props: {
    id: String
  },
  data () {
    return {
      pload: true,
      rule: [
        { required: true, message: '必填项', trigger: 'blur' }
      ],
      show: true,
      selectTable: {}
    }
  },
  computed: {
    title () {
      if (this.id) {
        return '修改';
      }
      else {
        return '添加';
      }
    }
  },
  created () {
		if (this.id) {
			this.getData()
		} else {
		  this.pload = false
		}
  },
  watch: {
    show (newVal) {
      this.$parent.edit = false;
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        if (this.id) {
          await artModify(this.selectTable)
        }else {
          await artSave(this.selectTable)
        }
        this.$notify({title: '操作成功', type: 'success', duration: 1000})
        this.$parent.edit = false
        this.$emit('getData')
      } catch(e) {
        this.pload = false
      }
    },
    async getData(){
      this.pload = true
	    const Datas = await artGetById({
	      articleId: this.id
	    })
      this.selectTable = Datas
      this.pload = false
    }
  }
}
</script>